package com.alasbusy.alasbusyapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.alasbusy.alasbusyapp.R;
import com.alasbusy.alasbusyapp.activity.detail.HtmlCssDetailActivity;
import com.alasbusy.alasbusyapp.bean.HomeBean;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/10.
 */

public class HtmlCssAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<HomeBean> mData = new ArrayList<>();
    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE = 1;
    public static final int LOADING_NO = 2;
    //上拉加载更多状态-默认为0
    private int load_more_status = 0;
    private static final int TYPE_ITEM = 1;  //普通Item View
    private static final int TYPE_FOOTER = 2;  //底部FootView
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    private LayoutInflater mInflater;

    public HtmlCssAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
    }

    public void setList(List<HomeBean> list) {
        mData.clear();
        mData.addAll(list);
        this.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       if (viewType == TYPE_ITEM) {
            /*View view = mInflater.inflate(R.layout.visitor_reservation_item, parent, false);
            //这边可以做一些属性设置，甚至事件监听绑定
            //view.setBackgroundColor(Color.RED);
            ItemViewHolder itemViewHolder = new ItemViewHolder(view);
            return itemViewHolder;*/
           ItemViewHolder viewHolder = new ItemViewHolder(mInflater.inflate(R.layout.html_item_view, parent, false));
            return viewHolder;
        } else if (viewType == TYPE_FOOTER) {
            View foot_view = mInflater.inflate(R.layout.listview_footer, parent, false);
            //这边可以做一些属性设置，甚至事件监听绑定
            //view.setBackgroundColor(Color.RED);
            FootViewHolder footViewHolder = new FootViewHolder(foot_view);
            return footViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       if (holder instanceof ItemViewHolder) {
            ItemViewHolder viewHolder = (ItemViewHolder) holder;
            HomeBean homeBean = mData.get(position);
            viewHolder.setData(homeBean.getData(), position);
        } else {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            switch (load_more_status) {
                case PULLUP_LOAD_MORE:
                    footViewHolder.itemView.setVisibility(View.VISIBLE);
                    footViewHolder.pull_to_refresh_loadmore_text.setText("上拉加载...");
                    break;
                case LOADING_MORE:
                    footViewHolder.itemView.setVisibility(View.VISIBLE);
                    footViewHolder.pull_to_refresh_loadmore_text.setText("正在加载...");
                    break;
                case LOADING_NO:
                    footViewHolder.itemView.setVisibility(View.INVISIBLE);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size() + 1;
    }

    /**
     * 进行判断是普通Item视图还是FootView视图
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        // 最后一个item设置为footerView
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    /**
     * //上拉加载更多
     * PULLUP_LOAD_MORE=0;
     * //正在加载中
     * LOADING_MORE=1;
     * //加载完成已经没有更多数据了
     * NO_MORE_DATA=2;
     *
     * @param status
     */
    public void changeMoreStatus(int status) {
        load_more_status = status;
        notifyDataSetChanged();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView homeitem_title;
        TextView homeitem_content;
        TextView homeitem_name;
        TextView homeitem_time;
        TextView homeitem_count;
        ImageView homeitem_img;

        public ItemViewHolder(View itemView) {
            super(itemView);
            homeitem_title = itemView.findViewById(R.id.homeitem_title);
            homeitem_content = itemView.findViewById(R.id.homeitem_content);
            homeitem_name = itemView.findViewById(R.id.homeitem_name);
            homeitem_time = itemView.findViewById(R.id.homeitem_time);
            homeitem_count = itemView.findViewById(R.id.homeitem_count);
            homeitem_img = itemView.findViewById(R.id.homeitem_img);
        }

        public void setData(HomeBean.DataBean data, int position) {
            HomeBean.DataBean.ItemdataBean itemdata = data.getItemdata();
            homeitem_title.setText(itemdata.getTitle());
            homeitem_content.setText(itemdata.getContent());
            homeitem_name.setText(itemdata.getName());
            homeitem_time.setText(itemdata.getTime());
            homeitem_count.setText(itemdata.getCount());
            Glide.with(mContext)
                    .load(itemdata.getImg())
                    //.diskCacheStrategy(DiskCacheStrategy.ALL)//缓存多个尺寸
                    .thumbnail(0.1f)//先显示缩略图  缩略图为原图的1/10
                    //.error(R.mipmap.logo)
                    .into(/*new GlideDrawableImageViewTarget(*/homeitem_img) /*{
                        @Override
                        public void onLoadStarted(Drawable placeholder) {
                            super.onLoadStarted(placeholder);
                        }

                        @Override
                        public void onLoadFailed(Exception e, Drawable errorDrawable) {
                            super.onLoadFailed(e, errorDrawable);
                        }

                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                            super.onResourceReady(resource, animation);
                        }
                    })*/;

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mContext.startActivity(new Intent(mContext,HtmlCssDetailActivity.class));
                }
            });
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder {
        TextView pull_to_refresh_loadmore_text;

        public FootViewHolder(View itemView) {
            super(itemView);
            pull_to_refresh_loadmore_text = (TextView) itemView.findViewById(R.id.pull_to_refresh_loadmore_text);
        }
    }

}
